package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.RechargeBean;
import com.sskj.mine.asset.data.RechargeTypeBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/05/03
 */
public class RechargeActivity extends BaseActivity<RechargePresenter> {

    @BindView(R2.id.select_coin)
    HeySpinner selectCoin;
    @BindView(R2.id.qr_code)
    ImageView qrCode;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.copy_tv)
    TextView copyTv;
    @BindView(R2.id.right_tv)
    TextView rightTv;
    private Disposable typeDispo;
    private String coinType;
    private String pid;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    public void initView() {
        setTitle(getString(R.string.mine_rechargeActivity1));
        selectCoin.setItemGravity(Gravity.CENTER);

        rightTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.mine_icon_history, 0);
        rightTv.setVisibility(View.VISIBLE);

        ClickUtil.click(rightTv, () -> ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withInt(Constans.TYPE, 1).navigation());

    }

    @Override
    public void initData() {
        mPresenter.getType();
    }

    public void refresh() {
        mPresenter.recharge(coinType);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        context.startActivity(intent);
    }

    public void updateType(List<RechargeTypeBean> data) {
        if (data==null||data.size()<=0)
            return;
        pid = data.get(0).getPid();
        coinType = data.get(0).getCoin_type();
        refresh();
        typeDispo = Flowable.fromIterable(data)
                .map(RechargeTypeBean::getPname)
                .toList()
                .subscribe((strings, throwable) -> {
                    selectCoin.attachData(strings);
                    selectCoin.setOnSelectListener(position -> {
                        pid = data.get(position).getPid();
                        coinType = data.get(position).getCoin_type();
                        refresh();
                    });

                });
    }

    public void updateUI(RechargeBean data) {
        address.setText(data.getAddress());
        ImageUtil.setImage(data.getQrc(), qrCode);
        ClickUtil.click(copyTv, () -> {
            CopyUtil.copy(data.getAddress());
        });
    }
}
